package com.atguigu.distributed.lock.leecode.midsearch;

/**
 * 比较版本号
 */
public class VersionNum {
    public int compare(String version1, String version2) {
        // write code here
        String[] nums1 = version1.split("\\.");
        String[] nums2 = version2.split("\\.");
        for (int i = 0; i < nums1.length || i < nums2.length; i++) {
            String str1 = i < nums1.length ? nums1[i] : "0";
            String str2 = i < nums2.length ? nums2[i] : "0";
            long num1 = 0;
            for (int j = 0; j < str1.length(); j++) {
                num1 = num1 * 10 + (str1.charAt(j) - '0');
            }
            long num2 = 0;
            for (int j = 0; j < str2.length(); j++) {
                num2 = num2 * 10 + (str2.charAt(j) - '0');
            }
            if (num1 > num2) {
                return 1;
            }
            if (num1 < num2) {
                return -1;
            }
        }
        //版本相同
        return 0;
    }
}
